/*
 * Copyright 2012 Benoît Combe <benoit.combe@gmail.com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package fr.ipst.spot.formulaires.entites;

import fr.ipst.spot.entites.Email;
import fr.ipst.spot.exceptions.SpotExceptionForm;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Benoît Combe <benoit.combe@gmail.com>
 */
public class FormEmail extends FormAbstractDocument<Email> {

    private String contenu;
    private String sender;
    private String receiver;
    private String cc;
    private String cci;

    /**
     *
     * @param entity
     */
    public FormEmail(Email entity) {
        super(new FormEmailId(entity.getId()), entity);
    }

    /**
     *
     * @param request
     */
    public FormEmail(HttpServletRequest request) {
        super(new FormEmailId(request), request);
    }

    @Override
    protected void validate(boolean creation) throws SpotExceptionForm {
        boolean erreur = true;
        try {
            super.validate(creation);
        } catch (SpotExceptionForm e) {
            erreur = true;
            this.concatErreurMsg(e);
        }
        try {
            this.isLongText(contenu, "contenu");
        } catch (SpotExceptionForm e) {
            erreur = true;
            this.concatErreurMsg(e);
        }
        try {
            this.isDefaultLengthOneLine(sender, "sender");
        } catch (SpotExceptionForm e) {
            erreur = true;
            this.concatErreurMsg(e);
        }
        try {
            this.isDefaultLengthOneLine(receiver, "receiver");
        } catch (SpotExceptionForm e) {
            erreur = true;
            this.concatErreurMsg(e);
        }
        if (!cc.isEmpty()) {
            try {
                this.isDefaultLengthOneLine(cc, "cc");
            } catch (SpotExceptionForm e) {
                erreur = true;
                this.concatErreurMsg(e);
            }
        }
        if (!cci.isEmpty()) {
            try {
                this.isDefaultLengthOneLine(cci, "cci");
            } catch (SpotExceptionForm e) {
                erreur = true;
                this.concatErreurMsg(e);
            }
        }
        if (erreur) {
            throw new SpotExceptionForm(msgErreurValidate);
        }
    }

    @Override
    public Email getEntityToPersist() throws SpotExceptionForm {
        return this.garnir(new Email(), true);
    }

    /**
     *
     * @param entityToFeed
     * @return
     * @throws SpotExceptionForm
     */
    @Override
    protected Email garnir(Email entityToFeed, boolean creation) throws SpotExceptionForm {
        entityToFeed = super.garnir(entityToFeed, creation);
        entityToFeed.setCc(cc);
        entityToFeed.setCci(cci);
        entityToFeed.setContenu(contenu);
        entityToFeed.setReceiver(receiver);
        entityToFeed.setSender(sender);
        return entityToFeed;
    }

    /**
     *
     * @return
     */
    public String getCc() {
        return cc;
    }

    /**
     *
     * @param cc
     */
    public void setCc(String cc) {
        this.cc = cc;
    }

    /**
     *
     * @return
     */
    public String getCci() {
        return cci;
    }

    /**
     *
     * @param cci
     */
    public void setCci(String cci) {
        this.cci = cci;
    }

    /**
     *
     * @return
     */
    public String getContenu() {
        return contenu;
    }

    /**
     *
     * @param contenu
     */
    public void setContenu(String contenu) {
        this.contenu = contenu;
    }

    /**
     *
     * @return
     */
    public String getReceiver() {
        return receiver;
    }

    /**
     *
     * @param receiver
     */
    public void setReceiver(String receiver) {
        this.receiver = receiver;
    }

    /**
     *
     * @return
     */
    public String getSender() {
        return sender;
    }

    /**
     *
     * @param sender
     */
    public void setSender(String sender) {
        this.sender = sender;
    }
}
